local renzu = fk.CreateSkill {
  name = "lingling__renzu",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable {
  ["lingling__renzu"] = "华夏人祖",
  [":lingling__renzu"] = "锁定技，出牌阶段开始时，你随机获得两个“出牌阶段限一次”的技能，回合结束时失去。",

  ["@lingling__renzu"] = "人祖"
}

renzu:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(renzu.name) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = {}
    for g, general in pairs(Fk.generals) do
      if not table.contains(Fk:currentRoom().disabled_generals, g) and general.package.extensionName == "lingling" then
        for _, s in ipairs(general:getSkillNameList()) do
          if not player:hasSkill(s, true) and string.find(Fk:translate(":" .. s, "zh_CN"), "出牌阶段限一次") then
            table.insertIfNeed(skills, s)
          end
        end
      end
    end
    local gets = table.random(skills, 2)
    room:setPlayerMark(player, "@lingling__renzu", gets)
    room:handleAddLoseSkills(player, gets[1] .. "|" .. gets[2])
  end,
})

renzu:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(renzu.name) and player:getMark("@lingling__renzu") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, name in ipairs(player:getMark("@lingling__renzu")) do
      room:handleAddLoseSkills(player, "-" .. name)
    end
    room:setPlayerMark(player, "@lingling__renzu", 0)
  end
})

renzu:addLoseEffect(function(self, player, is_death)
  local room = player.room
  for _, name in ipairs(player:getMark("@lingling__renzu")) do
    room:handleAddLoseSkills(player, "-" .. name)
  end
  room:setPlayerMark(player, "@lingling__renzu", 0)
end)

return renzu
